package com.ssi.myshop.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.ssi.myshop.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    private State currentState = State.NONE;
    private View mSuccessView;
    private View mLoadingView;
    private View mLoadErrorView;
    private View mLoadEmptyView;
    private FrameLayout mBaseContainer;
    private Unbinder mBind;


    public enum State {
        NONE, SUCCESS, LOADING, ERROR, Empty
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = lodeRootView(inflater, container);
        mBaseContainer = rootView.findViewById(R.id.base_container);
        loadStateView(inflater, container);
        mBind = ButterKnife.bind(this, rootView);
        initView(rootView);
        initListener();
        initPresenter();
        loadData();
        return rootView;
    }

    protected void loadData() {

    }


    protected void initPresenter() {

    }


    protected void initListener() {

    }


    protected void initView(View rootView) {

    }

    /**
     * 状态view
     *
     * @param inflater
     * @param container
     */
    private void loadStateView(LayoutInflater inflater, ViewGroup container) {

        mLoadingView = loadLoadingView(inflater, container);
        mBaseContainer.addView(mLoadingView);

        mSuccessView = LoadSuccessView(inflater, container);
        mBaseContainer.addView(mSuccessView);

        mLoadEmptyView = loadEmptyView(inflater, container);
        mBaseContainer.addView(mLoadEmptyView);

        mLoadErrorView = loadErrorView(inflater, container);
        mBaseContainer.addView(mLoadErrorView);

        setUpState(State.NONE);
    }

    protected void setUpState(State state) {
        this.currentState = state;
        mLoadingView.setVisibility(currentState == State.LOADING ? View.VISIBLE : View.GONE);
        mSuccessView.setVisibility(currentState == State.SUCCESS ? View.VISIBLE : View.GONE);
        mLoadEmptyView.setVisibility(currentState == State.Empty ? View.VISIBLE : View.GONE);
        mLoadErrorView.setVisibility(currentState == State.ERROR ? View.VISIBLE : View.GONE);
    }

    private View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_error, container, false);
    }

    private View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty, container, false);
    }

    private View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading, container, false);
    }

    private View LoadSuccessView(LayoutInflater inflater, ViewGroup container) {
        int resId = getRootViewId();
        View view = inflater.inflate(resId, container, false);
        return view;
    }

    protected abstract int getRootViewId();

    protected View lodeRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_fragment_layout, container, false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        release();
        if (mBind != null) {
            mBind.unbind();
        }
    }

    protected void release() {

    }
}
